package com.example.apidocserver.application.event.impl;

import com.example.apidocserver.application.dto.UserDocInfoDto;
import com.example.apidocserver.application.event.DocumentEvent;
import com.example.apidocserver.domain.document.entity.ApiDocument;
import com.example.apidocserver.domain.document.entity.ApiInfo;
import com.example.apidocserver.domain.document.entity.DocMenu;
import com.example.apidocserver.domain.document.event.ApiInfoDomainEvent;
import com.example.apidocserver.domain.document.event.DocumentDomainEvent;
import com.example.apidocserver.domain.user.entity.DocUsrHis;
import com.example.apidocserver.domain.user.event.UserDocInfoEventService;
import com.example.apidocserver.infrastructure.enums.HisIdEnum;
import com.example.apidocserver.infrastructure.util.user.UserDocAuthUtil;
import com.example.apidocserver.infrastructure.util.user.UserUtil;
import com.example.apidocserver.infrastructure.util.result.ParamException;
import com.example.apidocserver.interfaces.vo.DocumentVo;
import com.example.apidocserver.interfaces.vo.OrderChangeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 文档相关处理事件
 *
 * @author xyqlg
 */
@Service
public class DocumentEventImpl implements DocumentEvent {
    @Resource
    private DocumentDomainEvent documentDomainEvent;

    @Resource
    private ApiInfoDomainEvent apiInfoDomainEvent;

    @Resource
    private UserDocInfoEventService userDocInfoEventService;

    @Override
    public DocumentVo getReadDocuments(Integer spaceId, Boolean writeFlag, Boolean pubOnlyFlag) {
        // 0.创建返回对象
        DocumentVo documentVo = new DocumentVo();
        documentVo.setCreateFlag(UserUtil.createDocumentFlag()).setUpdateFlag(true);

        // 1. 获取用户日志信息 设置用户是否有创建文档权限、习惯i文档权限
        UserDocAuthUtil.currentDocInfo().setWriteFlag(writeFlag).setPublishFlag(pubOnlyFlag);
        DocUsrHis usrLastInfo = userDocInfoEventService.getUsrLastInfoBySpace(spaceId);
        if (Objects.isNull(usrLastInfo.getSpaceId())) {
            usrLastInfo.setSpaceId(spaceId);
        }

        // 2.文档信息，查询用户最后访问工作空间文档
        documentDomainEvent.setCurrentSpaceDocs(documentVo, usrLastInfo);

        // 3.当前文档目录，查询当前文档目录
        documentDomainEvent.setCurrentMenu(documentVo, usrLastInfo);

        // 4.当前接口信息，查询当前接口信息
        apiInfoDomainEvent.setApiInfoAllByUser(documentVo, usrLastInfo);

        // 5.更新用户访问日志
        userDocInfoEventService.setHisId(usrLastInfo, HisIdEnum.DOC);
        return documentVo;
    }

    @Override
    public DocumentVo getDocumentMenu(Integer docId, Integer envId, boolean writeFlag, Boolean pubOnlyFlag) {
        // 0.创建返回对象
        DocumentVo documentVo = new DocumentVo();

        // 1.TODO 获取用户日志信息 设置用户是否有创建文档权限、习惯i文档权限
        UserDocInfoDto userDocInfoDto = UserDocAuthUtil.currentDocInfo();
        userDocInfoDto.setWriteFlag(writeFlag).setPublishFlag(pubOnlyFlag);

        userDocInfoEventService.getUsrLastInfo().setDocId(docId).setEnvId(envId);
        DocUsrHis usrLastInfo = userDocInfoEventService.getUsrLastInfoByDocEnv();

        // 2.查询文档信息
        documentVo.setCurrentDocument(documentDomainEvent.findDocById(docId));
        if (Objects.isNull(documentVo.getCurrentDocument())) {
            return documentVo;
        }

        // 3.设置当前文档目录
        documentDomainEvent.setCurrentMenu(documentVo, usrLastInfo);

        // 4.设置当前接口信息
        apiInfoDomainEvent.setApiInfoAllByUser(documentVo, usrLastInfo);

        // 5.设置用户访问
        userDocInfoEventService.setHisId(usrLastInfo, HisIdEnum.DOC);
        return documentVo;
    }

    @Override
    public ApiDocument addDoc(ApiDocument document) {
        DocUsrHis usrLastInfo = userDocInfoEventService.getUsrLastInfo();
        ApiDocument apiDocument = documentDomainEvent.addDoc(document, usrLastInfo);
        usrLastInfo.setDocId(apiDocument.getId()).setApiId(null).setHisId(null).setId(null);
        userDocInfoEventService.setHisId(usrLastInfo, HisIdEnum.DOC);
        return apiDocument;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DocMenu upsertDocMenu(DocMenu docMenu, ApiInfo apiInfoIn) {
        // 1.查询文档信息
        ApiDocument apiDocument = documentDomainEvent.findDocById(docMenu.getDocId());
        if (Objects.isNull(apiDocument)) {
            throw new ParamException("文档信息已不存在。");
        }

        // 2.获取接口信息，如果有接口信息
        if (!Objects.isNull(apiInfoIn)) {
            apiInfoIn.setSpaceId(apiDocument.getSpaceId());
            apiInfoIn = apiInfoDomainEvent.upsertApiInfo(apiInfoIn);
            docMenu.setApiId(apiInfoIn.getId());
        } else {
            docMenu.setApiId(null);
        }

        // 3.新增文档目录信息
        return documentDomainEvent.upsertDocMenu(docMenu);
    }

    @Override
    public String orderChangeMenu(OrderChangeVo params) {
        documentDomainEvent.orderChangeMenu(params);
        return "ok.";
    }

    @Override
    public void deleteMenu(Integer id) {
        documentDomainEvent.deleteMenu(id);
    }
}
